package com.springboot.others.service.impl;

import com.springboot.others.entity.User;
import com.springboot.others.mapper.UserMapper;
import com.springboot.others.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "user")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Cacheable
    public User getUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @CachePut
    public User saveUser(User user) {
        User user1 = userMapper.insert(user);
        return user1;
    }

    @Override
    @CacheEvict(allEntries = true)
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void deleteUser(int id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Cacheable
    public List<User> findAllUser() {
        return userMapper.selectAll();
    }
}
